# -*- coding: utf-8 -*-
from datetime import datetime

from werkzeug.security import generate_password_hash, check_password_hash

from info import db


class BaseModel(object):
    """模型基类，为每个模型补充创建时间与更新时间"""
    create_time = db.Column(db.DateTime, default=datetime.now)  # 创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 更新时间


class Category(BaseModel, db.Model):
    """分类"""
    __tablename__ = "food_category"

    id = db.Column(db.Integer, primary_key=True)  # 分类id
    category_name = db.Column(db.String(64), nullable=False)  # 分类名字
    # 分类下的所有美食 category.food_list
    food_list = db.relationship('Food', backref='category', lazy='dynamic')

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "category_name": self.category_name,
            "update_time": self.update_time.strftime("%Y-%m-%d %H:%M:%S"),
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S")
        }
        return resp_dict


class Food(BaseModel, db.Model):
    """美食"""
    __tablename__ = "food"

    id = db.Column(db.Integer, primary_key=True)
    food_name = db.Column(db.String(256), nullable=False)
    price = db.Column(db.String(64), nullable=False)
    taste = db.Column(db.String(64), nullable=False)
    cooking_style = db.Column(db.String(64), nullable=False)
    sales = db.Column(db.Integer, default=0)
    food_image_url = db.Column(db.String(256))
    food_introduce = db.Column(db.String(1000), nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey("food_category.id"))
    order_lists = db.relationship('OrderList', backref='food', lazy='dynamic')

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "food_name": self.food_name,
            "price": self.price,
            "taste": self.taste,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "update_time": self.update_time.strftime("%Y-%m-%d %H:%M:%S"),
            "sales": self.sales,
            "cooking_style": self.cooking_style,
            "food_introduce": self.food_introduce,
            "food_image_url": self.food_image_url,
            "category": self.category.to_dict() if self.category else None
        }
        return resp_dict


class OrderList(BaseModel, db.Model):
    """订单"""
    __tablename__ = "order_list"

    id = db.Column(db.Integer, primary_key=True)
    food_id = db.Column(db.Integer, db.ForeignKey("food.id"))
    count = db.Column(db.Integer, default=0)
    username = db.Column(db.String(64), nullable=False)
    order_list_num = db.Column(db.String(64), nullable=False)
    phone_number = db.Column(db.String(64), nullable=False)
    address = db.Column(db.String(64), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    remark = db.Column(db.Text, nullable=False)
    price = db.Column(db.Integer, default=0)
    sum_price = db.Column(db.Integer, default=0)

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "food_id": self.food_id,
            "count": self.count,
            "username": self.username,
            "order_list_num": self.order_list_num,
            "phone_number": self.phone_number,
            "address": self.address,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "sum_price": self.sum_price,
            "price": self.price,
            "remark": self.remark,
            "food": self.food.to_dict(),

        }
        return resp_dict


class Admin(BaseModel, db.Model):
    """管理员"""
    __tablename__ = "admin"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)

    # password
    @property
    def password(self):
        raise AttributeError("")

    @password.setter
    def password(self, value):
        # self.password_hash
        self.password_hash = generate_password_hash(value)

    def check_password(self, password):
        """"""
        return check_password_hash(self.password_hash, password)

    def to_admin_dict(self):
        resp_dict = {
            "id": self.id,
            "username": self.username,
            "password_hash": self.password_hash,
        }
        return resp_dict
